package com.application.fabric.加密算法;

import org.junit.jupiter.api.Test;

public class 凯撒 {
    public static String encrypt(String plaintext, int shift) {
        StringBuilder ciphertext = new StringBuilder();
        for (char c : plaintext.toCharArray()) {
            if (Character.isLetter(c)) {
                char base = Character.isUpperCase(c) ? 'A' : 'a';
                c = (char) (((c - base + shift) % 26) + base);
            }
            ciphertext.append(c);
        }
        return ciphertext.toString();
    }

    public static String decrypt(String ciphertext, int shift) {
        return encrypt(ciphertext, 26 - shift);
    }

    public static void main(String[] args) {
        String originalString = "Hello, world!";
        int shift = 3;
        String encryptedString = encrypt(originalString, shift);
        System.out.println("Encrypted string: " + encryptedString);
        String decryptedString = decrypt(encryptedString, shift);
        System.out.println("Decrypted string: " + decryptedString);
    }

}
